﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Ionic.Zip;

namespace PlainOldVersionControl.Data.Base
{
    public class ObjectStoreDA
    {
        public static void ArchiveFile(string srcFile, string dstHash)
        {
            string subFolder = EnsureSubFoldersAndReturnSecondSubFolder(dstHash);
            string dstFileName = dstHash.Substring(4);
            string fullDstFile = Path.Combine(subFolder, dstFileName);

            if (!File.Exists(fullDstFile))
            {
                File.Copy(srcFile, fullDstFile);
            }
        }

        public static void RetreiveFile(string srcHash, string dstFile)
        {            
            string subFolder = EnsureSubFoldersAndReturnSecondSubFolder(srcHash);
            string srcFileName = srcHash.Substring(4);
            string fullSrcFile = Path.Combine(subFolder, srcFileName);

            File.Copy(fullSrcFile, dstFile, true);            
        }

        public static string GetFileContent(string blobHash)
        {            
            string subFolder = EnsureSubFoldersAndReturnSecondSubFolder(blobHash);
            string srcFileName = blobHash.Substring(4);
            string fullSrcFile = Path.Combine(subFolder, srcFileName);
            return File.ReadAllText(fullSrcFile);            
        }

        public static byte[] GetBlobsBytes(string blobHash)
        {            
            string subFolder = EnsureSubFoldersAndReturnSecondSubFolder(blobHash);
            string srcFileName = blobHash.Substring(4);
            string fullSrcFile = Path.Combine(subFolder, srcFileName);
            if (File.Exists(fullSrcFile))
                return File.ReadAllBytes(fullSrcFile);
            return null;            
        }

        public static string GetSHA1Hash(string pathName)
        {
            byte[] arrbytHashValue;
            FileStream oFileStream = null;
            SHA1CryptoServiceProvider oSha1Hasher = new SHA1CryptoServiceProvider();

            try
            {
                oFileStream = GetFileStream(pathName);
                arrbytHashValue = oSha1Hasher.ComputeHash(oFileStream);
            }
            finally
            {
                if (oFileStream != null)
                    oFileStream.Close();
            }

            return BitConverter.ToString(arrbytHashValue).Replace("-", "");
        }

        public static string AppendHash(string hashA, string hashB)
        {
            MemoryStream stream = null;
            byte[] arrbytHashValue;
            SHA1CryptoServiceProvider oSha1Hasher = new SHA1CryptoServiceProvider();

            try
            {
                stream = new MemoryStream(Encoding.UTF8.GetBytes(hashA + hashB));
                arrbytHashValue = oSha1Hasher.ComputeHash(stream);
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return BitConverter.ToString(arrbytHashValue).Replace("-", "");
        }

        public static void BackupStore(string dstFile)
        {
            using (ZipFile zip = new ZipFile())
            {
                zip.AddDirectory(Settings.Root);
                zip.Comment = "Plain Old Version Control Backup : " + DateTime.Now.ToString("G");
                zip.Save(dstFile);
            }
        }

        #region Private Methods

        private static FileStream GetFileStream(string pathName)
        {
            return (new FileStream(pathName, FileMode.Open,
                                   FileAccess.Read, FileShare.ReadWrite));
        }

        private static string EnsureSubFoldersAndReturnSecondSubFolder(string hash)
        {
            string first = Path.Combine(Settings.ObjectStoreRoot, hash.Substring(0, 2));
            string second = Path.Combine(first, hash.Substring(2, 2));

            if (!Directory.Exists(first))
                Directory.CreateDirectory(first);
            if (!Directory.Exists(second))
                Directory.CreateDirectory(second);

            return second;
        }

        #endregion
    }
}
